package com.zut.controller;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zut.mapper.LinkMapper;
import com.zut.pojo.Link;
import com.zut.pojo.Result;
import com.zut.pojo.VipLink;
import com.zut.service.LinkService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.websocket.server.PathParam;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

/**
 * @author: Mr.Liu
 * @create: 2021-11-13 13:12
 */
@Api(tags = "LinkController")
@Controller
public class LinkController {

    private static final Logger logger = LoggerFactory.getLogger(LinkController.class);

    @Autowired
    LinkService linkService;

    @Autowired
    LinkMapper linkMapper;

    @ApiOperation(value = "根据长链接生成短链接")
    @ResponseBody
    @PostMapping("/link/create")
    public Result createLinkId(String longLink, HttpServletRequest request) {
        logger.info("longLink:{}", longLink);
        return linkService.createLinkId(longLink, request);

    }

    @ApiOperation(value = "根据ID查找并重定向")
    @GetMapping("/find/{id}")
    public String find(@PathVariable String id) {
        logger.info("id:{}", id);
        if (id == null) {
            return "error";
        }
        return linkService.findId(id);
    }

    @ApiOperation("返回所有链接信息")
    @ResponseBody
    @GetMapping("/link/all/{current}")
    public Result getAll(@PathVariable Integer current) {
        return linkService.getAllLink(current);
    }

    @ApiOperation("根据id删除链接")
    @ResponseBody
    @PostMapping("/link/delete")
    public Result deleteLinkById(String id, HttpServletRequest request) {
        return linkService.deleteLink(id, request);
    }

    @ApiOperation("定制短链接")
    @ResponseBody
    @PostMapping("/link/vip")
    public Result vipLink(@RequestBody VipLink vipLink, HttpServletRequest request) {
        return linkService.createVipLink(vipLink.getId(), vipLink.getLongLink(), request);
    }

    @ApiOperation("Top10排行榜")
    @ResponseBody
    @GetMapping("/link/top")
    public List<Link> topTen() {
        return linkService.topTen();
    }

    @ApiOperation("根据用户的id返回用户个人的短链接")
    @ResponseBody
    @GetMapping("/link/user/{current}")
    public Result getLinkByUserId(HttpServletRequest request,@PathVariable Integer current) {
        return linkService.getLinkByUserId(request,current);
    }




}
